<?php
// export stuff as a text file

// Initialize a session.
session_name('NEWNASSOCVISITID');
session_start();

// is the user logged in?
if (!isset($_SESSION['user_level'])) {
  $page_title = "File download";
  include ('./includes/header.html');
  echo '<p>Sorry, you are not logged in.</p>';
  include ('./includes/footer.html');
  exit(); // Quit the script.
} 
// is the user an admin?
if ($_SESSION['user_level'] < 10) {
  $page_title = "File download";
  include ('./includes/header.html');
  echo '<p>Sorry, you do not have permission to do this.</p>';
  include ('./includes/footer.html');
  exit(); // Quit the script.
} 

 // they are logged in, so we can continue
require_once ('./mysql_connect.php'); // Connect to the database.
// first we're going to construct the contents
include ('./includes/admin.inc.php');
$sort_field = 'name';
if (isset($_GET['sort'])) {$sort_field = ($_GET['sort']);}
$show = 'all';
if (isset($_GET['show'])) {$show = ($_GET['show']);}
$user_results = fetch_export_details($show);

ob_start();
echo "\"first_name\",\"last_name\",\"title\",\"address1\",\"address2\",\"address3\",\"address4\",\"post_code\",\"home_phone\",\"work_phone\",\"mobile_phone\",\"email\"\n";
while ($row=mysql_fetch_array($user_results)) { 
echo "\"{$row['first_name']}\",\"{$row['last_name']}\",\"{$row['title']}\",\"{$row['address1']}\",\"{$row['address2']}\",\"{$row['address3']}\",\"{$row['address4']}\",\"{$row['post_code']}\",\"{$row['home_phone']}\",\"{$row['work_phone']}\",\"{$row['mobile_phone']}\",\"{$row['email']}\"\n";
}

mysql_close();  // close the db connection
// now we do the headers 
header ("Content-type: text/csv\n");
header ("Content-disposition: attachment; filename=\"nass-addresses.csv\"\n");
header('Content-Length: ' . ob_get_length());

// output the content
ob_end_flush();

?>